package com.power.sifa.approve.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.approve.dao.Apl_agentDao;
import com.power.sifa.approve.dao.Apl_applicantDao;
import com.power.sifa.approve.dao.Apl_arriveDao;
import com.power.sifa.approve.model.Apl_agent;
import com.power.sifa.approve.model.Apl_applicant;
import com.power.sifa.approve.model.Apl_arrive;
import com.power.sifa.approve.model.pageInfo.Apl_arrivePageInfo;
import com.power.sifa.approve.service.Apl_agentService;
import com.power.sifa.approve.service.Apl_applicantService;
import com.power.sifa.approve.service.Apl_arriveService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 送达信息业务层实现
 */
@Service
public class Apl_arriveServiceImpl extends BaseServiceImpl implements Apl_arriveService {

    @Resource
    private Apl_arriveDao dao;
    @Resource
    private Apl_applicantDao apl_applicantDao;
    @Resource
    private Apl_agentDao apl_agentDao;

    @Resource
    private Apl_applicantService apl_applicantService;

    @Resource
    private Apl_agentService apl_agentService;

    @Override
    public Apl_arrive getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Apl_arrive apl_arrive) {
        // save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (apl_arrive.getPrimaryKey() != null) {
            Apl_arrive apl_arrivefDb = dao.findById(apl_arrive.getPrimaryKey()).orElse(null);
            if (apl_arrivefDb != null) {
                BeanUtils.copyProperties(apl_arrive, apl_arrivefDb);
                dao.save(apl_arrivefDb);
                return;
            }
        }
        dao.save(apl_arrive);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Apl_arrive> findAll(Apl_arrivePageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if ("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        } else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber() <= 1 ? 0 : pageInfo.getPageNumber() - 1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }

    @Override
    public List<Apl_arrive> findAll() {
        return dao.findAll();
    }

    @Override
    public void saveAll(List<Apl_arrive> list) {
        dao.saveAll(list);
    }

    @Override
    public List<Map<String, String>> queryDeliveryPersonnel(String applyid) {
        List<Map<String, String>> result = new ArrayList<>(10);
        Map<String, String> tempMap;
        // 申请人
        List<Apl_applicant> appList = this.apl_applicantDao.getByApplyid(applyid);
        if (!CollectionUtils.isEmpty(appList)) {
            for (Apl_applicant apl_applicant : appList) {
                tempMap = JSON.parseObject(JSON.toJSONString(apl_applicant), Map.class);
                tempMap.put("value", apl_applicant.getId());
                tempMap.put("label", apl_applicant.getName());
                result.add(tempMap);
            }
        }
        // 代理人
        List<Apl_agent> ageList = this.apl_agentDao.getByApplyid(applyid);
        if (!CollectionUtils.isEmpty(ageList)) {
            for (Apl_agent apl_agent : ageList) {
                tempMap = JSON.parseObject(JSON.toJSONString(apl_agent), Map.class);
                tempMap.put("value", apl_agent.getId());
                tempMap.put("label", apl_agent.getName());
                result.add(tempMap);
            }
        }
        return result;
    }

    /**
     * 查询送达信息-带出受送达人
     * @param applyid
     * @return
     */
    @Override
    public Apl_arrive queryArriveByApplyid(String applyid) {
        Apl_arrive arrive = this.getById(applyid);
        if (ObjectUtil.isNotNull(arrive)) {
            // 查询受送达人 -- 可以为申请人 也可以为代理人
            Apl_applicant applicant = this.apl_applicantService.getById(arrive.getName());
            Apl_agent agent = this.apl_agentService.getById(arrive.getName());
            if (ObjectUtil.isNotNull(applicant)) {
                arrive.setApplicant(applicant);
            }
            if (ObjectUtil.isNotNull(agent)) {
                arrive.setAgent(agent);
            }
        }
        return arrive;
    }
}
